/**
 * 
 */
package com.sophossystems.client.asmon.service.impl;

import java.util.List;

import org.apache.log4j.Logger;

import com.sophossystems.client.asmon.dao.RoleDao;
import com.sophossystems.client.asmon.dto.RoleDTO;
import com.sophossystems.client.asmon.dto.RolePkDTO;
import com.sophossystems.client.asmon.exceptions.BusinessException;
import com.sophossystems.client.asmon.service.AdministrationServices;

/**
 * @author juanvelezballesteros
 * 
 */
public class AdministrationServicesImpl implements AdministrationServices {

	private RoleDao roleDao;

	private static Logger log = Logger
			.getLogger(AdministrationServicesImpl.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.sophossystems.client.asmon.service.AdministrationServices#registarRole
	 * (com.sophossystems.client.asmon.dto.RoleDTO)
	 */

	@Override
	public RoleDTO registarRole(RoleDTO rol) throws BusinessException {

		RoleDTO result = null;
		log.debug("AdministrationServicesImpl - registarRole");
		try {
			RolePkDTO pk = roleDao.insert(rol);
			if (pk != null) {
				result = roleDao.findByPrimaryKey(pk.getIdRole());
			} else {
				throw new BusinessException(" Error en la insercion de role");
			}
		} catch (Exception ex) {
			log.debug("Error Services: " + ex.getCause());
			throw new BusinessException("BusinessException " + ex.getMessage());
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.sophossystems.client.asmon.service.AdministrationServices#modificarRoel
	 * (com.sophossystems.client.asmon.dto.RoleDTO)
	 */
	@Override
	public RoleDTO modificarRole(RoleDTO rol) throws BusinessException {
		// TODO Auto-generated method stub
		log.debug("AdministrationServicesImpl -modificarRole");
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.sophossystems.client.asmon.service.AdministrationServices#borrarRoel
	 * (com.sophossystems.client.asmon.dto.RoleDTO)
	 */
	@Override
	public RoleDTO borrarRole(RoleDTO rol) throws BusinessException {
		// TODO Auto-generated method stub
		log.debug("AdministrationServicesImpl -borrarRole");
		return null;
	}

	/**
	 * @return the roleDao
	 */
	public RoleDao getRoleDao() {
		return roleDao;
	}

	/**
	 * @param roleDao
	 *            the roleDao to set
	 */
	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.sophossystems.client.asmon.service.AdministrationServices#buscarRole
	 * (com.sophossystems.client.asmon.dto.RoleDTO)
	 */
	@Override
	public RoleDTO buscarRole(RoleDTO rol) throws BusinessException {

		RoleDTO result = null;
		log.debug("AdministrationServicesImpl - registarRole");
		try {
			result = roleDao.findByPrimaryKey(rol.getIdRole());
		} catch (Exception ex) {
			log.debug("Error Services: " + ex.getCause());
			throw new BusinessException("BusinessException " + ex.getMessage());
		}
		return result;
		
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.sophossystems.client.asmon.service.AdministrationServices#buscarRoleList
	 * ()
	 */
	@Override
	public List<RoleDTO> buscarRoleList() throws BusinessException {
		List<RoleDTO> result = null;
		log.debug("AdministrationServicesImpl - buscarRoleList");
		try {
			result = roleDao.findAll();
		} catch (Exception ex) {
			log.debug("Error Services: " + ex.getCause());
			throw new BusinessException("BusinessException " + ex.getMessage());
		}
		return result;	
	}
	
	
	//buscarRolePorNombre
	@Override
	public RoleDTO buscarRolePorNombre(RoleDTO rol) throws BusinessException {

		RoleDTO result = null;
		log.debug("AdministrationServicesImpl - registarRole");
		try {
			result = roleDao.findWhereNameEquals(rol.getName()).get(0);
		} catch (Exception ex) {
			log.debug("Error Services: " + ex.getCause());
			throw new BusinessException("BusinessException " + ex.getMessage());
		}
		return result;
		
	}

}
